//
//  ViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/11/30.
//  Copyright © 2024 shipin. All rights reserved.
//

import CommonTools

class JCCPersonCenterViewController: UIViewController{
    
    @IBOutlet weak var headerImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var detailInfoLabel: UILabel!
    
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet weak var topBgView: UIView!
    
    @IBOutlet weak var editBtn: UIButton!
    
    @IBOutlet weak var view1: UIView!
    
    @IBOutlet weak var view2: UIView!
    @IBOutlet weak var view3: UIView!
    
    @IBOutlet weak var view4: UIView!
    
    @IBOutlet weak var view5: UIView!
    
    @IBOutlet weak var view6: UIView!
    
    
    @IBAction func popAction(_ sender: Any) {
        self.popAction()
    }
    
    
    
    override func viewWillAppear(_ animated: Bool) {
        
        self.navigationController?.navigationBar.isHidden = true
        let user = JCCManager.shared.user
        headerImageView.sd_setImage(with: URL(string: user.headimg), placeholderImage: UIImage(named: "icon_placeholder"))
        nameLabel.text = user.realname
        detailInfoLabel.text = "\(user.showGender) | \(user.age)岁 | \(user.remark)"
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }
    
    
    func setUI() {
        let view = UILabel()
        view.frame = CGRect(x: 0, y: -64, width: WIDTH, height: 255)
        view.backgroundColor = .white
        self.view.insertSubview(view, at: 0)
        
        let layer1 = CALayer()
        layer1.backgroundColor = UIColor(red: 237/255, green: 243/255, blue: 250/255, alpha: 1).cgColor
        layer1.bounds = view.bounds
        layer1.position = view.center
        view.layer.addSublayer(layer1);
        
        let layer2 = CAGradientLayer()
        layer2.colors = [
            UIColor(red: 0, green: 217/255, blue: 202/255, alpha:0.5).cgColor,
            UIColor(red: 80/255.0, green: 137/255, blue: 250/255, alpha:0.5).cgColor
        ]
        layer2.locations = [0,1]
        layer2.startPoint = CGPoint(x: 0, y: 0.5)
        layer2.endPoint = CGPoint(x: 1, y: 0.5)
        layer2.bounds = view.bounds
        layer2.position = view.center
        view.layer.addSublayer(layer2);
        
        
        
        self.topBgView.layer.cornerRadius = 11
        self.editBtn.layer.cornerRadius = 5
        self.headerImageView.clipsToBounds = true
        self.headerImageView.layer.cornerRadius = 77/2
        editBtn.addBlock(for: .touchUpInside) { _ in
            let vc = JCCUpdateUserInfoViewController()
            self.show(vc, sender: nil)
        }
        
        self.setRadius(targetView: self.view1)
        self.setRadius(targetView: self.view2)
        self.setRadius(targetView: self.view3)
        self.setRadius(targetView: self.view4)
        self.setRadius(targetView: self.view5)
        self.setRadius(targetView: self.view6)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.scrollView.contentSize = CGSize(width: WIDTH,height: HEIGHT + 100)
        setTitle("登陆注册")
        setUI()
    }
    

    
    func setRadius(targetView:UIView){
        targetView.layer.cornerRadius = 11
        targetView.clipsToBounds = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickAcion))
        targetView.addGestureRecognizer(tap)
    }
    func showLogoutConfirmation() {
        let alert = UIAlertController(title: "退出登录", message: "您确定要退出登录吗？", preferredStyle: .alert)
        
        // 取消按钮
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        // 确认按钮
        let logoutAction = UIAlertAction(title: "确认", style: .destructive) { _ in
            JCCTools.clearToken()
            self.navigationController?.popToRootViewController(animated: true)
        }
        
        // 添加按钮到警告框
        alert.addAction(cancelAction)
        alert.addAction(logoutAction)
        
        // 显示警告框
        present(alert, animated: true, completion: nil)
    }
    @objc func clickAcion (gesture:UITapGestureRecognizer) {
        if (gesture.view?.tag == 6){
            showLogoutConfirmation()
        }
        else{
            var identifier:String = ""
            let tag = gesture.view?.tag ?? 0
            switch tag {
            case 1:
                identifier = "JCCMoneyViewController"
                break
            case 2:
                identifier = "JCCCollectViewController"
                break
            case 3:
                showPrivacy(type: .privacy)
                return
            case 4:
                identifier = "JCCHistoryViewController"
                break
            default:
                identifier = ""
                break
            }
            if (identifier != ""){
                if let viewController = self.storyboard?.instantiateViewController(withIdentifier: identifier) {
                    self.navigationController?.pushViewController(viewController, animated: true)
                }
            }
            
        }
    }
}

